/*  
 * i-OSGi - Tunable Bundle Isolation for OSGi
 * Copyright (C) 2011  Sven Schulz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.iosgi.ie.os;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Sven Schulz
 */
public class Scancodes {

	private static int SHIFT_DOWN = 0x2a;
	private static int KEY_UP = 0x80;

	private static char[] chars = new char[] { '1', '2', '3', '4', '5', '6',
			'7', '8', '9', '0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o',
			'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c',
			'v', 'b', 'n', 'm', ' ', '=', '\n', ';', '/', '.', ':' };

	private static int[][] scancode = new int[][] { { 0x02 }, { 0x03 },
			{ 0x04 }, { 0x05 }, { 0x06 }, { 0x07 }, { 0x08 }, { 0x09 },
			{ 0x0a }, { 0x0b }, { 0x10 }, { 0x11 }, { 0x12 }, { 0x13 },
			{ 0x14 }, { 0x15 }, { 0x16 }, { 0x17 }, { 0x18 }, { 0x19 },
			{ 0x1e }, { 0x1f }, { 0x20 }, { 0x21 }, { 0x22 }, { 0x23 },
			{ 0x24 }, { 0x25 }, { 0x26 }, { 0x2c }, { 0x2d }, { 0x2e },
			{ 0x2f }, { 0x30 }, { 0x31 }, { 0x32 }, { 0x39 }, { 0x0d },
			{ 0x1c }, { 0x27 }, { 0x35 }, { 0x34 }, { SHIFT_DOWN, 0x27 } };

	private static Map<Character, int[]> codes;

	static {
		codes = new HashMap<Character, int[]>();
		for (int i = 0; i < chars.length; i++) {
			codes.put(chars[i], scancode[i]);
		}
	}

	public static int[] getScancodes(char c) {
		int[] sc = codes.get(c);
		int[] withUps = new int[2 * sc.length];
		for (int i = 0; i < withUps.length; i++) {
			withUps[i] = sc[i % sc.length] + (i >= sc.length ? KEY_UP : 0);
		}
		return withUps;
	}
}